package com.sscaipiao.android.activity.ShuangSeQiu;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.Base.NormalActivity;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.utils.MathUtil;
import com.sscaipiao.android.utils.ShakeListener;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.widget.BallBase;
import com.sscaipiao.android.widget.BlueBall;
import com.sscaipiao.android.widget.RedBall;

public class DoubleColorBallActivity extends NormalActivity implements OnClickListener, OnItemClickListener, BallBase.onClickCallback {
	private List<Integer> redBallIds = new ArrayList<Integer>();
	private List<Integer> blueBallIds = new ArrayList<Integer>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContainerMain(R.layout.double_color_ball);
		setupUI(true,false,true,true,true);
		setTitleText("双色球");

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());

		setLottInformation(getIntent(), "ssq");

		String sAgeFormat1 = getResources().getString(R.string.double_color_ball_hint);
		setMoney(String.format(sAgeFormat1, "0", "0"));

		addShakeListener();
		addBallsID();
		addBallsCallback();
		if (getLottLostValues_DoubleBall()) {
			setupLostValues();
		}
	}
	private void addShakeListener() {
		ShakeListener shakeListener = new ShakeListener(this);
		shakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
			public void onShake() {
				randomChooseBalls();
			}

		});
	}

	private void setupLostValues() {
		int redBallNum = redBallIds.size();
		for (int i = 0; i < lostValues.size(); i++) {
			String  text = lostValues.get(i);
			if (i < redBallNum) {
				getRedBallByIndex(i).setLabel(text);
			} else {
				getBlueBallByIndex(i-redBallNum).setLabel(text);
			}

		}
	}


	@Override
	public void onBallClick(View ball) {
		int redNum = queryChoseRedBallNum();
		if (redNum > 20) {
			Toast.makeText(this, "你选择的红球数已经超过20个，不能再选",Toast.LENGTH_SHORT).show();
			BallBase curBall = (BallBase) ball;
			curBall.reset();
			return;
		}
		reCalculatorSum();
	}

	private BallBase getRedBallByIndex(int index) {
		return  (RedBall)findViewById(redBallIds.get(index));
	}

	private BallBase getBlueBallByIndex(int index) {
		return  (BlueBall)findViewById(blueBallIds.get(index));
	}

	private void addBallsCallback() {
		BallBase Ball;
		for (int i = 0; i < redBallIds.size(); i++) {
			Ball = getRedBallByIndex(i);
			Ball.setCallback(this);
		}
		for (int i = 0; i < blueBallIds.size(); i++) {
			Ball = getBlueBallByIndex(i);
			Ball.setCallback(this);
		}
	}

	private void addBallsID() {
		redBallIds.add(R.id.double_color_ball_red_ball_01);
		redBallIds.add(R.id.double_color_ball_red_ball_02);
		redBallIds.add(R.id.double_color_ball_red_ball_03);
		redBallIds.add(R.id.double_color_ball_red_ball_04);
		redBallIds.add(R.id.double_color_ball_red_ball_05);
		redBallIds.add(R.id.double_color_ball_red_ball_06);
		redBallIds.add(R.id.double_color_ball_red_ball_07);
		redBallIds.add(R.id.double_color_ball_red_ball_08);
		redBallIds.add(R.id.double_color_ball_red_ball_09);
		redBallIds.add(R.id.double_color_ball_red_ball_10);
		redBallIds.add(R.id.double_color_ball_red_ball_11);
		redBallIds.add(R.id.double_color_ball_red_ball_12);
		redBallIds.add(R.id.double_color_ball_red_ball_13);
		redBallIds.add(R.id.double_color_ball_red_ball_14);
		redBallIds.add(R.id.double_color_ball_red_ball_15);
		redBallIds.add(R.id.double_color_ball_red_ball_16);
		redBallIds.add(R.id.double_color_ball_red_ball_17);
		redBallIds.add(R.id.double_color_ball_red_ball_18);
		redBallIds.add(R.id.double_color_ball_red_ball_19);
		redBallIds.add(R.id.double_color_ball_red_ball_20);
		redBallIds.add(R.id.double_color_ball_red_ball_21);
		redBallIds.add(R.id.double_color_ball_red_ball_22);
		redBallIds.add(R.id.double_color_ball_red_ball_23);
		redBallIds.add(R.id.double_color_ball_red_ball_24);
		redBallIds.add(R.id.double_color_ball_red_ball_25);
		redBallIds.add(R.id.double_color_ball_red_ball_26);
		redBallIds.add(R.id.double_color_ball_red_ball_27);
		redBallIds.add(R.id.double_color_ball_red_ball_28);
		redBallIds.add(R.id.double_color_ball_red_ball_29);
		redBallIds.add(R.id.double_color_ball_red_ball_30);
		redBallIds.add(R.id.double_color_ball_red_ball_31);
		redBallIds.add(R.id.double_color_ball_red_ball_32);
		redBallIds.add(R.id.double_color_ball_red_ball_33);

		blueBallIds.add(R.id.double_color_ball_blue_ball_01);
		blueBallIds.add(R.id.double_color_ball_blue_ball_02);
		blueBallIds.add(R.id.double_color_ball_blue_ball_03);
		blueBallIds.add(R.id.double_color_ball_blue_ball_04);
		blueBallIds.add(R.id.double_color_ball_blue_ball_05);
		blueBallIds.add(R.id.double_color_ball_blue_ball_06);
		blueBallIds.add(R.id.double_color_ball_blue_ball_07);
		blueBallIds.add(R.id.double_color_ball_blue_ball_08);
		blueBallIds.add(R.id.double_color_ball_blue_ball_09);
		blueBallIds.add(R.id.double_color_ball_blue_ball_10);
		blueBallIds.add(R.id.double_color_ball_blue_ball_11);
		blueBallIds.add(R.id.double_color_ball_blue_ball_12);
		blueBallIds.add(R.id.double_color_ball_blue_ball_13);
		blueBallIds.add(R.id.double_color_ball_blue_ball_14);
		blueBallIds.add(R.id.double_color_ball_blue_ball_15);
		blueBallIds.add(R.id.double_color_ball_blue_ball_16);
	}


	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (R.id.btn_show_history == v.getId()) {
			showLottHistory(DoubleColorBallHisActivity.class);
		} else if (R.id.btn_radom == v.getId()) {
			randomChooseBalls();
		} else if (R.id.im_undo == v.getId()) {
			resetAllBalls();
			reCalculatorSum();
		} else if (R.id.btn_confirm == v.getId()) {
			doConfirm();
		}
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();

	}

	private int queryChoseRedBallNum() {
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < redBallIds.size(); i++) {
			ball = getRedBallByIndex(i);
			if (ball.isSelect()) {
				nBallNum ++;
			}
		}
		return nBallNum;
	}
	private int queryChoseBlueBallNum() {
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < blueBallIds.size(); i++) {
			ball = getBlueBallByIndex(i);
			if (ball.isSelect()) {
				nBallNum ++;
			}
		}
		return nBallNum;
	}
	private List<Integer> queryChoseRedBallArray() {
		List<Integer> ChooseBalls = new ArrayList<Integer>();
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < redBallIds.size(); i++) {
			ball = (BallBase) findViewById(redBallIds.get(i));
			if (ball.isSelect()) {
				ChooseBalls.add(i+1);
			}
		}
		return ChooseBalls;
	}
	private List<Integer> queryChoseBlueBallArray() {
		List<Integer> ChooseBalls = new ArrayList<Integer>();
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < blueBallIds.size(); i++) {
			ball = (BallBase) findViewById(blueBallIds.get(i));
			if (ball.isSelect()) {
				ChooseBalls.add(i+1);
			}
		}
		return ChooseBalls;
	}
	private List<String> queryChoseRedBallArray_Name() {
		List<String> ChooseBalls = new ArrayList<String>();
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < redBallIds.size(); i++) {
			ball = (BallBase) findViewById(redBallIds.get(i));
			if (ball.isSelect()) {
			    ChooseBalls.add(ball.getBallText());
			}
		}
		return ChooseBalls;
	}
	private List<String> queryChoseBlueBallArray_Name() {
		List<String> ChooseBalls = new ArrayList<String>();
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < blueBallIds.size(); i++) {
			ball = (BallBase) findViewById(blueBallIds.get(i));
			if (ball.isSelect()) {
				ChooseBalls.add(ball.getBallText());
			}
		}
		return ChooseBalls;
	}
	private void doConfirm() {
		int nRedNum = queryChoseRedBallNum();
		int nBlueNum = queryChoseBlueBallNum();
		List<Integer> redChooseBalls = queryChoseRedBallArray();
		List<Integer> blueChooseBalls = queryChoseBlueBallArray();

		if (nBlueNum < 1 || nRedNum < 6) {
			Toast.makeText(DoubleColorBallActivity.this, "请选择6个或以上红球, 和1个或以上蓝球", Toast.LENGTH_SHORT).show();
		} else {
			List<String> choosen = user.getChoosen();
			if (choosen == null) {
				choosen = new ArrayList<String>();
			}
			String redBalls = "";
			String blueBalls = "";
			String singleOrMult = "";

			String sAgeFormat1 = getResources().getString(R.string.double_color_ball_add_1);
			String number = String.format(sAgeFormat1, notecount() + "");

			for (int num : redChooseBalls) {
				if (num < 10) {
					redBalls += ("0" + num + Constants.STRING_SPLITE);
				} else {
					redBalls += (num + Constants.STRING_SPLITE);
				}

			}
			for (int num : blueChooseBalls) {
				if (num < 10) {
					blueBalls += ("0" + num + Constants.STRING_SPLITE);
				} else {
					blueBalls += (num + Constants.STRING_SPLITE);
				}

			}

			if (notecount() == 1) {
				singleOrMult = getResources().getString(R.string.double_color_ball_add_single);
			} else {
				singleOrMult = getResources().getString(R.string.double_color_ball_add_multiple);
			}


			String str = redBalls.substring(0, redBalls.length() - 1) + Constants.STRING_SPLITE_COMPLEX +
					blueBalls.substring(0, blueBalls.length() - 1) + Constants.STRING_SPLITE_COMPLEX +
					singleOrMult + Constants.STRING_SPLITE_COMPLEX + number;

			if (!choosen.contains(str)) {
				choosen.add(str);
				user.setChoosen(choosen);
				user.setHistory(history);

				Intent intent = new Intent(DoubleColorBallActivity.this, DoubleColorBallPayActivity.class);
				Bundle b = new Bundle();
				b.putSerializable(Constants.USER, user);
				b.putSerializable("date", date);
				b.putSerializable("leftTime", leftTime);
				intent.putExtras(b);
				startActivity(intent);

			} else {
				Toast.makeText(DoubleColorBallActivity.this, "该组合已在购买列表中请重新选择", Toast.LENGTH_SHORT).show();
				return;
			}


		}


	}

	private int notecount() {    //计算下的注数
		int redNum = queryChoseRedBallNum();
		int blueNum = queryChoseBlueBallNum();
		int redLottNum = (int) MathUtil.calcCNM(redNum, 6);
		int blueLottNum = queryChoseBlueBallNum();
		return redLottNum * blueLottNum;
	}

	/**
	 *
	 */
	private void randomChooseBalls() {

		resetAllBalls();

		Integer[] randomRedBalls = StringUtil.getRandom(33, 6);
		Integer[] randomBlueBalls = StringUtil.getRandom(16, 1);

		for (int num : randomRedBalls) {
			RedBall Ball = (RedBall) (findViewById(redBallIds.get(num-1)));
			Ball.setSelected(true);
		}

		for (int num : randomBlueBalls) {
			BlueBall Ball = (BlueBall) (findViewById(blueBallIds.get(num-1)));
			Ball.setSelected(true);
		}
		reCalculatorSum();
	}


	/**
	 *
	 */
	private void reCalculatorSum() {
		String sAgeFormat1 = getResources().getString(R.string.double_color_ball_hint);
		//2元  x %1$s注 x 1倍 = %2$s 元　
		int notecount = notecount();
		setMoney(String.format(sAgeFormat1, notecount + "", (notecount * 2) + ""));
	}


	private void resetAllBalls() {
		for (int i = 0; i < redBallIds.size(); i++) {
			RedBall ball = (RedBall) findViewById(redBallIds.get(i));
			ball.reset();
		}
		for (int i = 0; i < blueBallIds.size(); i++) {
			BlueBall ball = (BlueBall) findViewById(blueBallIds.get(i));
			ball.reset();
		}

	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}


}
